package com.timber.util;
 /**
  * Usage example:
  *
  *    TablePrinterButton.createTablePrinterButton(
  *           new SLTablePrinter(
  *                      < _TableView | _TableModel>,
  *                      "Some Title",
  *                      SLTablePrinter.<LANDSCAPE|PORTRET|REVERSED_LANDSCAPE>),
  *           <boolean: isLargeButton?>,
  *           <boolean: WithDialog?>)
  *
  * 1.
  *    pPanel.add(TablePrinterButton.createTablePrinterButton( aTablePrinter ));
  *
  * 2.
  *    pPanle.add(TablePrinterButton.createTablePrinterButton( aTablePrinter, true, true ));
  *
  * 3.
  *    pPanle.add(TablePrinterButton.createTablePrinterButton( aTablePrinter, true, true, 50, 50 ));
  *
  */

import com.timber.util.*;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;

public class TablePrinterButton extends SLImageButton
{

  public static TablePrinterButton createTablePrinterButton(SLTablePrinter aTP) {
    return new TablePrinterButton(aTP);
  }

  public static TablePrinterButton createTablePrinterButton(SLTablePrinter aTP, boolean largeButton, boolean isDialog) {
    return new TablePrinterButton(aTP, largeButton, isDialog);
  }

  public static TablePrinterButton createTablePrinterButton(SLTablePrinter aTP, boolean largeButton, boolean isDialog, int x, int y) {
    TablePrinterButton aPB = new TablePrinterButton(aTP, largeButton, isDialog);
    aPB.setSize(x, y);
    return aPB;
  }

  public static TablePrinterButton createCustomTablePrinterButton(SLTablePrinter aTP, boolean largeButton, boolean createListener, int x, int y){
    return createTablePrinterButton( aTP, x, y, largeButton, createListener );
  }

  public static TablePrinterButton createTablePrinterButton(SLTablePrinter aTP, int x, int y, boolean largeButton, boolean createListener ) {
    TablePrinterButton aPB = new TablePrinterButton(aTP, largeButton, false, createListener);
    aPB.setSize(x, y);
    return aPB;
  }

  private SLTablePrinter _tablePrinter = null;
  private TableModel _tableModel =  null;
  private String _title;
  private int _orientation;
  private boolean _isLarge;
  private boolean _isDialog;


  public TablePrinterButton(SLTablePrinter aTP)
  {
    this(aTP, false, false);
  }

  public TablePrinterButton(SLTablePrinter aTP, boolean largeButton, boolean isDialog)
  {
    this(aTP, largeButton, isDialog, true);
  }

  public TablePrinterButton(SLTablePrinter aTP, boolean largeButton, boolean isDialog, boolean createListener)
  {
    super();
    _tablePrinter = aTP;
    _isLarge = largeButton;
    _isDialog = isDialog;
    buttonSetup(createListener);
  }

  public SLTablePrinter getTablePrinter()
  {
    return _tablePrinter;
  }

  public void setTablePrinter(SLTablePrinter aTP)
  {
    if (aTP == null)
       throw new NullPointerException("Cannot use NULL SLTablePrinter!");
    _tablePrinter = aTP;
  }

  public void setSize(int x, int y){
    super.setSize(x, y);
    setPreferredSize(new Dimension( x , y ));
    setMinimumSize(new Dimension(x, y));
    setMaximumSize(new Dimension(x, y));
  }

  private void buttonSetup(boolean doListener) {

    ImageIcon aIcon;

    if (_isLarge) {
      aIcon = ImageMgr.getIcon( ImageMgr.PRINT_ICON );
      largeImgInit();
    } else {
      aIcon = ImageMgr.getIcon( ImageMgr.PRINT_ICON );
      smallImgInit();
    }

    setImageAt(aIcon.getImage(), 0);
    setAutoResize(false);
    setToolTipText("Print Table");

    if (!doListener) return;

    if (_isDialog){
        this.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(ActionEvent e) {
              _tablePrinter.setTitle( _title );
              _tablePrinter.printAsyncWithDialog((JFrame)null);
            }
        });
    }else this.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(ActionEvent e) {
              _tablePrinter.setTitle( _title );
              _tablePrinter.printAsync( (JFrame)null);
            }
        });
  }

  private void smallImgInit(){
      setPreferredSize(new Dimension(20,20));
  }

  private void largeImgInit(){
      setText("Print");
      setMinimumSize(new Dimension(32, 32));
      setPreferredSize(new Dimension(60, 45));
      setToolTipText("Print table");
      setMaximumSize(new Dimension(60, 50));
      setThreeButtonStates(true);
      setBackground(null);
      setFocusRect(false);
      setLAF( false );
      setOpaque(false);
      setForeground(Color.black);
  }

  public void setTitle(String s) { _title = s;}
  public String getTitle()       { return _title;}

}
